<?php
    class CategoryRepository
    {
        var $db;
        
        function CategoryRepository(){
            global $SERVER, $DATABASE, $USERNAME, $PASSWORD;
            $this->db = new Connection($SERVER, $DATABASE, $USERNAME, $PASSWORD);
        }
        
         function GetCategory($id){
            $query = "SELECT * FROM category WHERE ID_i =" . $id;
            $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
            if ($result) {
                $row = mysql_fetch_array($result);
                return new CategoryEntity($row["ID_i"], $row["Name_s"],
                        $row["Description_s"], $row["SectionID_i"]);
            }
            return null;
        }
        
        function GetAll()
        {
            if(isset($this->db))
            {
                $result = $this->db->ExecuteQuery("select * from Category");
                return $this->ChangeToArray($result);
            }
        }
        
        function GetCategoriesBySectionID($sectionID)
        {
            if(isset($this->db))
            {
                $result = $this->db->ExecuteQuery("select * from Category where SectionID_i = " . $sectionID);
                return $this->ChangeToArray($result);
            }
        }
        
        function ChangeToArray($result)
        {
            $array = array();
            $count = 0;
            while($row = mysql_fetch_array($result, MYSQL_NUM))
            {
                $entity = new CategoryEntity($row[0], $row[1], $row[2], $row[3]); 
                $array[$count++] = $entity;
            }
            return $array;
        }
        
         public function GetCategoryFromSection($id)
        {
        	$query = "SELECT * FROM category WHERE SectionID_i LIKE '" . $id . "'";
        	$result = $this->db->ExecuteQuery($query) or die("Failed with error: " . mysql_error());
       	 	if ($result) 
       	 	{
            	$row = mysql_fetch_array($result);
            	return new CategoryEntity($row["ID_i"], $row["Name_s"],
                    $row["Description_s"], $row["SectionID_i"]);
       		 }
       		return null;
    	 }

        public function GetNameCategoryByID() {
        $query = "SELECT * FROM category WHERE ID_i='1'";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
        $array = array();
        $count = 0;
        while ($row = mysql_fetch_array($result)) {
//            	return new CategoryEntity($row["ID_i"], $row["Name_s"],
//                    $row["Description_s"], $row["SectionID_i"]);

            return new CategoryEntity($row["ID_i"], $row["Name_s"],
                    $row["Description_s"], $row["SectionID_i"]);
            $array[$count++] = $entity;
        }
        return $array;
    }
    
        function Insert(CategoryEntity $entity)
        {
            $query = "INSERT INTO category(Name_s, Description_s, SectionID_i) VALUES(";
            $query.="'".$entity->Name_s."',";
            $query.="'".$entity->Description_s."',";
            $query.="'".$entity->SectionID_i."')";
            return $this->db->ExecuteQuery($query);
        }
        
        function Delete($id)
        {
            $query = "DELETE FROM category WHERE ID_i=" . $id;
            return $this->db->ExecuteQuery($query);
        }
    
        function Update(CategoryEntity $entity)
        {
            $query = "UPDATE category SET ";
            $query .= "Name_s='" . $entity->Name_s . "',";
            $query .= "Description_s='" . $entity->Description_s . "',";
            $query .= "SectionID_i='" . $entity->SectionID_i . "'";
            $query .= " WHERE ID_i=" . $entity->ID_i;
            return $this->db->ExecuteQuery($query);
        }
    }
?>